package com.base.myproject.server;

import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;
import nl.justobjects.pushlet.util.Rand;


public class HeartBeatServlet{
	
	  static public class CometRatePull extends EventPullSource{

	        @Override
	        protected long getSleepTime() {
	            return 3000L; //这里是设置了一个轮回的时间间隔
	        }

	        @Override
	        protected Event pullEvent() {
	            Event event = Event.createDataEvent("/CometDemo/TestRate"); //这里创建一个服务。

	            event.setField("JPY",Rand.randomInt(1, 100)); //以下是发送到客户端的数据
	            event.setField("GBR",Rand.randomInt(1, 100));
	            event.setField("RMB",Rand.randomInt(1, 100));
	            event.setField("USD",Rand.randomInt(1, 100));
	            System.out.println("ewwe");
	            return event;
	        }
	        
	    }
	  
	  static public class AEXStocksEventPullSource extends EventPullSource {

			String[] stocks = {"abn amro", "26",
					"aegon", "38",
					"ahold", "34",
					"akzo nobel", "51",
					"asm lith h", "26",
					"corus plc", "2",
					"dsm", "40",
					"elsevier", "14",
					"fortis (nl)", "32",
					"getronics", "6",
					"gucci", "94",
					"hagemeyer", "25",
					"heineken", "61",
					"ing c", "78",
					"klm", "66",
					"kon olie", "66",
					"kpn", "13",
					"numico c", "44",
					"philips, kon", "38",
					"tnt", "26",
					"unilever c", "62",
					"vendex kbb", "16",
					"vnu", "49",
					"wolt-kluw c", "25"};

			public long getSleepTime() {
				return Rand.randomLong(2000, 4000);
			}

			public Event pullEvent() {
				   System.out.println("pullEvent");
				Event event = Event.createDataEvent("/stocks/aex");
				int stockNumber = Rand.randomInt(0, (stocks.length) / 2 - 1);
				int nextStockIndex = 2 * stockNumber;

				event.setField("number", "" + stockNumber);
				event.setField("name", stocks[nextStockIndex]);
				if (stocks[nextStockIndex + 1] == null) {
					stocks[nextStockIndex + 1] = "" + Rand.randomInt(50, 150);
				}
				int currentStockValue = new Integer(stocks[nextStockIndex + 1]).intValue();
				int newStockValue = currentStockValue + Rand.randomInt(-2, 2);

				event.setField("rate", "" + newStockValue + "." + Rand.randomInt(0, 99));
				return event;
			}

		}
	
}

